/*
 * Copyright (C) 2011  Xu Haojie
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.poemcode.gzlib.search;

import java.util.LinkedList;
import java.util.List;




import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class SearchHandler extends DefaultHandler {
	
	private static final String TAG = "SearchHandler";
	
	private List<SearchResult> mResultList;
	
	private SearchResult mResult;
	
	private StringBuilder mBuilder;
	
	private Double mTotalRows;
	
	private Double mPageRows;
	
	/**
	 * get the list of results
	 * 
	 * @return mResultList	the list of result
	 */
	public List<SearchResult> getSearchResults() {
		return mResultList;
	}
	
	/**
	 * get the total of pages
	 * 
	 * @return the total of pages
	 */
	public int getTotalPageSize() {
		//TODO NullPointerException
		return Double.valueOf(Math.floor(mTotalRows / mPageRows)).intValue();
	}
	
	@Override
	public void startDocument() throws SAXException {
		mResultList = new LinkedList<SearchResult>();
		mBuilder = new StringBuilder();
	}

	@Override
	public void endDocument() throws SAXException {
		Log.d(TAG, "endDocument");
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equals("ROW") && attributes.getIndex("cnt") != -1) {
			mResult = new SearchResult();
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (mResult != null) {
			if (qName.equals("BOOKRECNO")) {
				mResult.setBookrecno(mBuilder.toString());
			} else if (qName.equals("AUTHOR")) {
				mResult.setAuthor(mBuilder.toString());
			} else if (qName.equals("CLASSNO")) {
				mResult.setClassno(mBuilder.toString());
			} else if (qName.equals("ISBN")) {
				mResult.setIsbn(mBuilder.toString());
			} else if (qName.equals("PAGE")) {
				mResult.setPage(mBuilder.toString());
			} else if (qName.equals("PRICE")) {
				mResult.setPrice(mBuilder.toString());
			} else if (qName.equals("PUBLISHER")) {
				mResult.setPublisher(mBuilder.toString());
			} else if (qName.equals("PUBDATE")) {
				mResult.setPubdate(mBuilder.toString());
			} else if (qName.equals("TITLE")) {
				mResult.setTitle(mBuilder.toString());
			} else if (qName.equals("SUBJECT")) {
				mResult.setSubject(mBuilder.toString());
			} else if (qName.equals("BOOKTYPE")) {
				mResult.setBooktype(mBuilder.toString());
			} if (qName.equals("ROW")) {
				mResultList.add(mResult);
				mResult = null;
			}
		}
		
		if (qName.equals("TOTALROWS")) {
			mTotalRows = Double.valueOf(mBuilder.toString());
		} else if (qName.equals("PAGEROWS")){
			mPageRows = Double.valueOf(mBuilder.toString());
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		mBuilder.delete(0, mBuilder.length());
		mBuilder.append(ch, start, length);
	}

}
